/**
 * Copyright (C) 2011-2012 Rustam Bogubaev
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 */
package com.google.code.myminimvc;

import com.google.code.myminimvc.controller.ControllerEngine;
import com.google.code.myminimvc.results.ForwardResult;
import com.google.code.myminimvc.results.RedirectResult;
import com.google.code.myminimvc.results.TilesResult;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.servlet.context.ServletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author rustam
 */
public class Filter implements javax.servlet.Filter {
    private final Logger logger = LoggerFactory.getLogger(Filter.class);
    private FilterConfig filterConfig = null;

    public void destroy() {
        logger.info(getClass().getName() + " destroyed");
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        logger.info(getClass().getName() + " initialized");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        Result result = new ControllerEngine(httpServletRequest, httpServletResponse).execute();

        if (result == null) {
            logger.debug("There was no any controller execution performed, so request and response passed to the next entity in the chain.");

            filterChain.doFilter(request, response);
            return;
        }

        if (result instanceof RedirectResult) {
            String resource = ((RedirectResult) result).getResource();
            logger.debug("redirecting to " + resource);

            httpServletResponse.sendRedirect(resource);
        } else if (result instanceof TilesResult) {
            String definition = ((TilesResult) result).getDefinition();
            logger.debug("tiles definition: " + definition);

            TilesContainer container = ServletUtil.getContainer(filterConfig.getServletContext());
            container.render(definition, request, response);
        } else if (result instanceof ForwardResult) {
            String resource = ((ForwardResult) result).getResource();
            logger.debug("forwarding to " + resource);

            filterConfig.getServletContext().getRequestDispatcher(resource).forward(request, response);
        }
    }
}
